home *** CD-ROM | disk | FTP | other *** search
- // Program Author: Paul Baxter
- // pbaxter@assistivetech.com
- //
-
- #include <DeskBus.h>
- #include "ShowInit.h"
-
-
- #ifndef powerc
- #pragma error Sorry this only works on a real machine
- #endif
-
- #define kKeyBoardADBAddress 2
-
- #define kIconID 128
- #define kNoInstallID 129
-
- #define kKeyStateMask 0x80
- #define kValueMask 0x7F
-
-
- #define kNumKeys 129
-
- // Where is this defined??
- extern ADBInitUPP JADBProc : 0x06B8;
-
- ADBInitUPP gOldADBInitProc;
- ADBServiceRoutineUPP gKeyBoardServiceRoutine = nil;
- ADBServiceRoutineUPP gOldKeyBoardServiceRoutine = nil;
- ADBDataBlock gKeyBoardADBinfo;
-
- extern Boolean InstallServiceRoutines(void);
- extern void InstallJADBProc(void);
- extern void MyADBInitProc(SInt8 callOrder);
- extern pascal void ADBKeyBoardServiceRoutine(Ptr buffer, TempADBServiceRoutineUPP completionProc, long refCon, long command);
- extern Boolean ExpandedMacro(char ch, Boolean up);
- extern void LoadMacros(void);
-
- #ifdef powerc
- // for Metrowerks' linker, this defines the interface for main().
- ProcInfoType __procinfo = kCStackBased | RESULT_SIZE(kNoByteCode);
- #endif
-
- Handle gMacros[kNumKeys];
-
- // Main entry point
- void main(void)
- {
- Handle ourhandle;
- Boolean InstalledOK = false;
- THz pZone;
-
- pZone = GetZone();
- SetZone(SystemZone());
- ourhandle = Get1IndResource('INIT', 1);
- if (ourhandle) {
- DetachResource(ourhandle);
- HLock(ourhandle);
- HNoPurge(ourhandle);
-
- LoadMacros();
-
- // take over the ADBServiceRoutine for the KeyBoard
- InstalledOK = InstallServiceRoutines();
- if (InstalledOK) {
- // Install a JADBProc so we don't go away after a ADBReInit
- InstallJADBProc();
- }
-
- }
- if (InstalledOK)
- ShowINIT(kIconID, -1);
- else
- ShowINIT(kNoInstallID, -1);
-
- SetZone(pZone);
- }
-
- // Setup our service routines
- Boolean InstallServiceRoutines(void)
- {
- long adbaddr;
- ADBDataBlock adb_data;
- ADBSetInfoBlock setADBInfo;
- short adbcount, adbindex;
- OSErr err;
-
- err = -1;
- adbcount = CountADBs();
- for (adbindex = 1; adbindex <= adbcount; adbindex++) {
- adbaddr = GetIndADB(&adb_data,adbindex);
-
- if ((adb_data.origADBAddr == kKeyBoardADBAddress)) {
- BlockMoveData(&adb_data, &gKeyBoardADBinfo, sizeof(ADBDataBlock));
- // Remember we will be called once to install and twice during each ADBReInit
- if (!gKeyBoardServiceRoutine) {
- gKeyBoardServiceRoutine = NewADBServiceRoutineProc(ADBKeyBoardServiceRoutine);
- }
- if (!gKeyBoardServiceRoutine)
- return err == noErr;
-
- gOldKeyBoardServiceRoutine = adb_data.dbServiceRtPtr;
- setADBInfo.siService = gKeyBoardServiceRoutine;
- setADBInfo.siDataAreaAddr = adb_data.dbDataAreaAddr;
-
- err = SetADBInfo(&setADBInfo, adbaddr);
- break;
- }
- }
- return err == noErr;
- }
-
- // Install a JADBProc callback for before and after ADBReInit
- void InstallJADBProc(void)
- {
- ADBInitUPP ourADBInitProc;
-
- gOldADBInitProc = JADBProc;
- ourADBInitProc = NewADBInitProc(MyADBInitProc);
- JADBProc = ourADBInitProc;
- }
-
-
- void LoadMacros(void)
- {
- short count, index, rfile, macroId;
- Handle theHandle;
- ResType rType;
- Str255 rName;
-
- for (count = 0; count < kNumKeys; count++) {
- gMacros[count] = nil;
- }
-
- rfile = CurResFile();
- count = Count1Resources('KMAC');
- for (index = 1; index <= count; index++) {
- UseResFile(rfile);
- theHandle = Get1IndResource('KMAC', index);
- if (theHandle) {
- HLock(theHandle);
- GetResInfo(theHandle, ¯oId, &rType, rName);
- gMacros[macroId] = theHandle;
- DetachResource(theHandle);
- }
- }
-
- }
-
- // Handler for KeyBoard service routine
- pascal void ADBKeyBoardServiceRoutine(Ptr buffer, TempADBServiceRoutineUPP completionProc, long refCon, long command)
- {
- #pragma unused (refCon)
- unsigned char keyupstate, count, macrosize, macroindex;
- Ptr macroPtr;
- char mybuffer[3],ch;
-
- for (count = 0; count < buffer[0]; count++) {
- // high most bit is the key state
- if (buffer[count + 1] != 0xFF) {
- keyupstate = buffer[count + 1] & kKeyStateMask;
- ch = buffer[count + 1] & kValueMask;
- if (gMacros[ch]) {
- buffer[count + 1] = 0xFF; // say that there is no data
- if (keyupstate == false) {
- macroPtr = *gMacros[ch];
- macrosize = *(short*)macroPtr;
- macroPtr += sizeof(short);
- for (macroindex = 0; macroindex < macrosize; macroindex ++) {
- mybuffer[0] = 2;
- mybuffer[1] = macroPtr[macroindex * 2] & kValueMask;
- if (macroPtr[(macroindex * 2) + 1]) {
- mybuffer[1] |= kKeyStateMask;
- }
- mybuffer[2] = 0xFF;
- CallADBServiceRoutineProc(gOldKeyBoardServiceRoutine, mybuffer, nil, (long)gKeyBoardADBinfo.dbDataAreaAddr, command);
- }
- }
- }
-
- }
- }
- CallADBServiceRoutineProc(gOldKeyBoardServiceRoutine, buffer, completionProc, (long)gKeyBoardADBinfo.dbDataAreaAddr, command);
- }
-
-
- // needed to re-install our service routine
- void MyADBInitProc(SInt8 callOrder)
- {
- Boolean InstallOK;
-
- CallADBInitProc(gOldADBInitProc, callOrder);
- if (callOrder)
- InstallOK = InstallServiceRoutines();
- }